package com.quickask.common;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by moonlight on 2015/9/4.
 */
public class Pager<T> {
    // The number of current page
    private int currentPage;

    //The number of records in one page
    private int pageSize;

    //The total number of records in DB
    private int total;

    //The total number of page
    private int pageCount;

    //The number where begin to get recocrd
    private int startRecord;

    //Whether it has previous page
    private boolean hasPreviousPage;

    //Whether it has next page
    private boolean hasNextPage;

    //Whether it has only one page
    private boolean onlyOnePage;

    //The records of page designed
    private List<T> pageRecords;

    /**
     * 构造方法，只构造空页
     */
    public Pager() {
        this(1, 0, 3, new ArrayList());
    }

    /**
     * 构造方法，只构造空页
     * @param currentPage
     * @param pageSize
     */
    public Pager(int currentPage, int pageSize) {
        this(currentPage,0,pageSize,new ArrayList());
    }

    /**
     * 默认构造方法
     * @param currentPage
     * @param totalSize
     * @param pageSize
     * @param data
     */
    public Pager(int currentPage, int totalSize, int pageSize,List data) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.total = totalSize;
        this.pageRecords = data;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        if (currentPage<1) {
            this.currentPage = 1;
            return;
        }
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        if (pageSize<=0) {
            this.pageSize = 1;
        } else {
            this.pageSize = pageSize;
        }
    }

    public int getTotal() {
        return total;
    }
    public void setTotal(int totalRecords) {
        this.total = totalRecords;
    }

    public int getPageCount() {
        if(total == 0) {
            pageCount = 1;
            return pageCount;
        }
        boolean isZero = (total%pageSize==0);
        pageCount = total/pageSize;
        pageCount = isZero?pageCount:pageCount+1;
        return pageCount;
    }

    public int getStartRecord() {
        startRecord = ((currentPage-1)*pageSize);
        return startRecord;
    }

    public boolean isHasPreviousPage() {
        hasPreviousPage = (currentPage==1)?false:true;
        return hasPreviousPage;
    }

    public boolean isHasNextPage() {
        hasNextPage = (currentPage == getPageCount())?false:true;
        return hasNextPage;
    }

    public boolean isOnlyOnePage() {
        onlyOnePage = (getPageCount()==1)?true:false;
        return onlyOnePage;
    }

    public List<T> getPageRecords() {
        return pageRecords;
    }

    public void setPageRecords(List<T> pageRecords) {
        this.pageRecords = pageRecords;
    }
}
